package carson.web.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 将 JavaScript 内容发送到响应
 *
 * @author Carson Chen
 */
public class JavaScriptResult extends AbstractResult {

    public JavaScriptResult(String javaScript) {
        this.javaScript = javaScript;
    }

    private String javaScript;

    /**
     * 获取 javascript
     *
     * @return javascript
     */
    public String getJavaScript() {
        return javaScript;
    }

    /**
     * 设置 javascript
     *
     * @param javaScript javascript
     */
    public void setJavaScript(String javaScript) {
        this.javaScript = javaScript;
    }

    @Override
    public void executeResult(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/javascript");
            PrintWriter pw = response.getWriter();
            pw.print(this.javaScript);
            pw.flush();
        } catch (IOException e) {

        }
    }
}
